package com.project.myxima.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.project.myxima.DetailActivity;
import com.project.myxima.R;
import com.project.myxima.adapters.AlbumListAdapter;
import com.project.myxima.interfaces.IRecommendViewCallback;
import com.project.myxima.presenters.AlbumDetailPresenter;
import com.project.myxima.presenters.RecommendPresenter;
import com.project.myxima.utils.LogUtils;
import com.project.myxima.views.UILoader;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;
import java.util.Objects;

public class RecommendFragment extends BaseFragment implements IRecommendViewCallback, UILoader.OnRetryClickListener , AlbumListAdapter.onRecommendItemClickListener {
    private static final String TAG = "RecommendFragment";
    private View mRootView;
    private RecyclerView mRecommendRv;
    private AlbumListAdapter mAlbumListAdapter;
    private RecommendPresenter mRecommendPresenter;
    private UILoader mUILoader;


    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, ViewGroup container) {

        mUILoader = new UILoader(Objects.requireNonNull(getContext())) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSuccessView(layoutInflater,container);//有不同
            }
        };
        //RecycleView的使用
            //找到控件
        //获取数据
        //getRecommendData();
            //先获取逻辑层的对象
        mRecommendPresenter = RecommendPresenter.getInstance();
            //注册通知接口
        mRecommendPresenter.registerViewCallback(this);
            //获取推荐列表
        mRecommendPresenter.getRecommendList();

        if (mUILoader.getParent() instanceof ViewGroup) {
            ((ViewGroup) mUILoader.getParent()).removeView(mUILoader);
        }

        //设置Retry监听器
        mUILoader.setOnRetryClickListener(this);

        //返回view，给界面显示
        return mUILoader;
    }

    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        LogUtils.d(TAG,"current thread --->" + Thread.currentThread().getName());
        mRootView = layoutInflater.inflate(R.layout.fragment_recommend,container,false);

        TwinklingRefreshLayout refreshLayout = mRootView.findViewById(R.id.over_scroll_rl);
        refreshLayout.setPureScrollModeOn();

        mRecommendRv = mRootView.findViewById(R.id.recommend_list);
        //设置RecycleView的管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecommendRv.setLayoutManager(layoutManager);
        /*mRecommendRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = UIUtil.dip2px(view.getContext(),5);
                outRect.bottom = UIUtil.dip2px(view.getContext(),5);
                outRect.left = UIUtil.dip2px(view.getContext(),5);
                outRect.right = UIUtil.dip2px(view.getContext(),5);
            }
        });*/
        //设置适配器
        mAlbumListAdapter = new AlbumListAdapter();
        mRecommendRv.setAdapter(mAlbumListAdapter);
        mAlbumListAdapter.setOnRecommendItemClickListener(this);

        return mRootView;
    }

    @Override
    public void onRecommendListLoaded(List<Album> result) {
        LogUtils.d(TAG,"RecommendListLoaded");
        //当获取到内容时，该方法被调用
        //更新UI
        mAlbumListAdapter.setData(result);
        mUILoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }

    @Override
    public void onNetworkError() {
        LogUtils.d(TAG,"NETWORK_ERROR");
        mUILoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
    }

    @Override
    public void onEmpty() {
        LogUtils.d(TAG,"EMPTY");
        mUILoader.updateStatus(UILoader.UIStatus.EMPTY);
    }

    @Override
    public void onLoading() {
        LogUtils.d(TAG,"LOADING");
        mUILoader.updateStatus(UILoader.UIStatus.LOADING);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消接口注册，以免内存泄漏
        if (mRecommendPresenter != null) {
            mRecommendPresenter.unregisterViewCallback(this);
        }
    }

    @Override
    public void onRetryClick() {
        //网络不佳时点击重试
        //重新获取即可
        if (mRecommendPresenter != null) {
            mRecommendPresenter.getRecommendList();
        }
    }

    @Override
    public void onItemClick(int position, Album album) {
        //根据位置拿到数据
        AlbumDetailPresenter.getInstance().setTargetAlbum(album);
        //item被点击时
        Intent intent = new Intent(getContext(), DetailActivity.class);
        startActivity(intent);

    }
}
